

clear all
use "./longbase3.dta"
set type double
xtset cnum time

* ayear: JFY (Japanese Fiscal Year)
gen amonth = L3.month
replace amonth = 10 if year == 1996 & month == 1
replace amonth = 11 if year == 1996 & month == 2
replace amonth = 12 if year == 1996 & month == 3
gen ayear = L3.year
replace ayear = 1995 if year == 1996 & month == 1
replace ayear = 1995 if year == 1996 & month == 2
replace ayear = 1995 if year == 1996 & month == 3
********************** GET exchange rate ***********************
merge 1:1 cnum time using jpylcc
drop if _merge ==2
drop _merge
rename c jpylcc
********************** GET DATA **********************************
* Target beef HS
local beef /// 
20110000 20120000 20120010 20120090 20130010 20130020 20130030 20130090 ///
20210000 20220000 20220010 20220090 20230010 20230020 20230030 20230090
* Other beef HS for quotas
local others ///
20610011 20610019 20621000 20622000 20629010 20629020 20629090 ///
160250510 160250520 160250590 160250600 160250700
* HS for FTA quotas (as indicated in JIKKOKANZEI tables where ambiguous)
local Mexico ///
20120000 20120010 20120090 20130010 20130020 20130030 20130090 20220000 20220010 ///
20220090 20230010 20230020 20230030 20230090 ///
20610011 20610019 20621000 20622000 20629010 20629020 20629090 ///
160250510 160250520 160250590 160250600 160250700
local Chile ///
20220000 20220010 20220090 20230010 20230020 20230030 20230090

* Obtain kjpy kgrm (Note: All3 contains monthly cumulative data)
foreach s of local beef{
gen hscode = `s'
merge 1:1 cnum time hscode using All3, keepusing(kg kjpy)
drop if _merge == 2
gen ckjpy`s' = kjpy
gen ckgrm`s' = kg
drop kjpy kg hscode _merge
      replace ckgrm`s' = 0 if missing(ckgrm`s')
	  replace ckjpy`s' = 0 if missing(ckjpy`s')
* Generate the frame
gen kgrm`s' =0
gen kjpy`s' =0
* Un-cumulate ckgrm ckjpy
sort cnum time
replace kgrm`s' = ckgrm`s' - L.ckgrm`s'
replace kgrm`s' = ckgrm`s' if month == 1
replace kjpy`s' = ckjpy`s' - L.ckjpy`s'
replace kjpy`s' = ckjpy`s' if month == 1
}
drop ckgrm* ckjpy*
foreach s of local others{
gen hscode = `s'
merge 1:1 cnum time hscode using All3, keepusing(kg)
drop if _merge == 2
gen ckgrm`s' = kg
drop kg hscode _merge
      replace ckgrm`s' = 0 if missing(ckgrm`s')
* Generate the frame
gen kgrm`s' =0
* Un-cumulate ckgrm 
sort cnum time
replace kgrm`s' = ckgrm`s' - L.ckgrm`s'
replace kgrm`s' = ckgrm`s' if month == 1
}
drop ckgrm*

 *Base Tariff rate
foreach s of local beef{
gen tr`s' = 50
}

foreach s of local beef{
replace tr`s' = 40.4 if ayear == 1999 
replace tr`s' = 42.3 if ayear == 1998 
replace tr`s' = 44.3 if ayear == 1997 
replace tr`s' = 46.2 if ayear == 1996 
replace tr`s' = 48.1 if ayear == 1995 
replace tr`s' = 38.5   if ayear >= 2000 
replace tr`s' = 0    if ayear >= 2007 & regime == "LDC" 
}

* SAFEGUARD
* 2017m8-2018m3  (excl. Mexico, Chile, Aussie) 38.5->50 (alic)
foreach s of local beef{
replace tr`s' = 50 if time >= 691 & time <= 698 & cnames != "Mexico" & cnames != "Chile" & cnames != "Australia"
}
* 2003m8-2004m3  38.5->50
foreach s of local beef{
replace tr`s' = 50 if time >= 523 & time <= 530 //
}


* CPTPP
local rate = 26.6
local ay = 2019
local rgm = "CPTPP"
foreach s of local beef{
replace tr`s' = `rate' if ayear == `ay' & regime == "`rgm'"
}
local rate = 25.8
local ay = 2020
local rgm = "CPTPP"
foreach s of local beef{
replace tr`s' = `rate' if ayear == `ay' & regime == "`rgm'"
}
* EU
local rate = 26.7
local ay = 2019
local rgm = "EU"
foreach s of local beef{
replace tr`s' = `rate' if ayear == `ay' & regime == "`rgm'"
}
local rate = 25.8
local ay = 2020
local rgm = "EU"
foreach s of local beef{
replace tr`s' = `rate' if ayear == `ay' & regime == "`rgm'"
}
* USA (Japan-US Agreement)
local rate = 25.8
local ay = 2020
foreach s of local beef{
replace tr`s' = `rate' if ayear == `ay' & regime == "`rgm'"
}
* Australia 
foreach ay in 2020 2019 2018 2017 2016 2015{
local rate = cond(`ay'==2020,1,0)*28.2 /// R2
            + cond(`ay'==2019,1,0)*28.8 /// R1
            + cond(`ay'==2018,1,0)*29.3 /// H30
            + cond(`ay'==2017,1,0)*29.9 /// H29
            + cond(`ay'==2016,1,0)*30.5 /// H28 
            + cond(`ay'==2015,1,0)*31.5
replace tr20110000 = `rate' if ayear == `ay' & cnames == "Australia"
replace tr20120000 = `rate' if ayear == `ay' & cnames == "Australia"
replace tr20130010 = `rate' if ayear == `ay' & cnames == "Australia"
replace tr20130020 = `rate' if ayear == `ay' & cnames == "Australia"
replace tr20130030 = `rate' if ayear == `ay' & cnames == "Australia"
replace tr20130090 = `rate' if ayear == `ay' & cnames == "Australia"
}
foreach ay in 2020 2019 2018 2017 2016 2015{
local rate = cond(`ay'==2020,1,0)*26.4 /// R2
            + cond(`ay'==2019,1,0)*26.7 /// R1
            + cond(`ay'==2018,1,0)*26.9 /// H30
            + cond(`ay'==2017,1,0)*27.2 /// H29
            + cond(`ay'==2016,1,0)*27.5 /// H28 
            + cond(`ay'==2015,1,0)*28.5
replace tr20210000 = `rate' if ayear == `ay' & cnames == "Australia"
replace tr20220000 = `rate' if ayear == `ay' & cnames == "Australia"
replace tr20230010 = `rate' if ayear == `ay' & cnames == "Australia"
replace tr20230020 = `rate' if ayear == `ay' & cnames == "Australia"
replace tr20230030 = `rate' if ayear == `ay' & cnames == "Australia"
replace tr20230090 = `rate' if ayear == `ay' & cnames == "Australia"
}

gsort cnames year month

* Quotas (Monthly cumulate from April to March in tons)
* Mexico
gen Mexico_q =0
foreach v of local Mexico{
* cumulate April to March
bysort ayear cnum (amonth) : gen ackgrm`v' = sum(kgrm`v')
replace Mexico_q = Mexico_q + ackgrm`v'/1000  // [tons]
}
drop ackgrm*
foreach ay in 2020 2019 2018 2017 2016 2015 2014 2013 2012 2011 2010 2009 2008 2007{
local quota = cond(`ay'==2020,1,0)*15000 /// R2
            + cond(`ay'==2019,1,0)*15000 /// R1
            + cond(`ay'==2018,1,0)*15000 /// H30
            + cond(`ay'==2017,1,0)*15000 /// H29
            + cond(`ay'==2016,1,0)*15000 /// H28 
            + cond(`ay'==2015,1,0)*15000 /// H27
            + cond(`ay'==2014,1,0)*13500 /// H26
			+ cond(`ay'==2013,1,0)*12000 /// H25
            + cond(`ay'==2012,1,0)*10500 /// H24
            + cond(`ay'==2011,1,0)*6000  /// H23
            + cond(`ay'==2010,1,0)*6000  /// H22
            + cond(`ay'==2009,1,0)*6000  /// H21
			+ cond(`ay'==2008,1,0)*4000  /// H20
            + cond(`ay'==2007,1,0)*3000
replace tr20120000 = 34.6 if ayear == `ay' & cnames == "Mexico" & Mexico_q <= `quota'
replace tr20130010 = 30.8 if ayear == `ay' & cnames == "Mexico" & Mexico_q <= `quota'
replace tr20130020 = 30.8 if ayear == `ay' & cnames == "Mexico" & Mexico_q <= `quota'
replace tr20130030 = 30.8 if ayear == `ay' & cnames == "Mexico" & Mexico_q <= `quota'
replace tr20130090 = 30.8 if ayear == `ay' & cnames == "Mexico" & Mexico_q <= `quota'
replace tr20220000 = 30.8 if ayear == `ay' & cnames == "Mexico" & Mexico_q <= `quota'
replace tr20230010 = 30.8 if ayear == `ay' & cnames == "Mexico" & Mexico_q <= `quota'
replace tr20230020 = 30.8 if ayear == `ay' & cnames == "Mexico" & Mexico_q <= `quota'
replace tr20230030 = 30.8 if ayear == `ay' & cnames == "Mexico" & Mexico_q <= `quota'
replace tr20230090 = 34.6 if ayear == `ay' & cnames == "Mexico" & Mexico_q <= `quota'
}
replace tr20120000 = 0 if ayear == 2006 & cnames == "Mexico" & Mexico_q <= 10
replace tr20130010 = 0 if ayear == 2006 & cnames == "Mexico" & Mexico_q <= 10
replace tr20130020 = 0 if ayear == 2006 & cnames == "Mexico" & Mexico_q <= 10
replace tr20130030 = 0 if ayear == 2006 & cnames == "Mexico" & Mexico_q <= 10
replace tr20130090 = 0 if ayear == 2006 & cnames == "Mexico" & Mexico_q <= 10
replace tr20220000 = 0 if ayear == 2006 & cnames == "Mexico" & Mexico_q <= 10
replace tr20230010 = 0 if ayear == 2006 & cnames == "Mexico" & Mexico_q <= 10
replace tr20230020 = 0 if ayear == 2006 & cnames == "Mexico" & Mexico_q <= 10
replace tr20230030 = 0 if ayear == 2006 & cnames == "Mexico" & Mexico_q <= 10
replace tr20230090 = 0 if ayear == 2006 & cnames == "Mexico" & Mexico_q <= 10

* Chile
gen Chile_q =0
foreach v of local Chile{
* cumulate April to March
bysort ayear cnum (amonth) : gen ackgrm`v' = sum(kgrm`v')
replace Chile_q = Chile_q + ackgrm`v'/1000
}
drop ackgrm*
foreach ay in 2020 2019 2018 2017 2016 2015 2014 2013 2012 2011 2010 2009 2008 2007{
local quota = cond(`ay'==2020,1,0)*4000 /// R2
            + cond(`ay'==2019,1,0)*4000 /// R1
            + cond(`ay'==2018,1,0)*4000 /// H30
            + cond(`ay'==2017,1,0)*4000 /// H29
            + cond(`ay'==2016,1,0)*4000 /// H28 
            + cond(`ay'==2015,1,0)*4000 /// H27
            + cond(`ay'==2014,1,0)*4000 /// H26
			+ cond(`ay'==2013,1,0)*4000 /// H25
            + cond(`ay'==2012,1,0)*4000 /// H24
            + cond(`ay'==2011,1,0)*4000  /// H23
            + cond(`ay'==2010,1,0)*3250  /// H22
            + cond(`ay'==2009,1,0)*2600  ///
            + cond(`ay'==2008,1,0)*1950  ///
            + cond(`ay'==2007,1,0)*650  
replace tr20220000 = 30.8 if ayear == `ay' & cnames == "Chile" & Chile_q <= `quota'
replace tr20230010 = 30.8 if ayear == `ay' & cnames == "Chile" & Chile_q <= `quota'
replace tr20230020 = 30.8 if ayear == `ay' & cnames == "Chile" & Chile_q <= `quota'
replace tr20230030 = 30.8 if ayear == `ay' & cnames == "Chile" & Chile_q <= `quota'
replace tr20230090 = 30.8 if ayear == `ay' & cnames == "Chile" & Chile_q <= `quota'
}
* overwrite tariff rates
foreach ay in 2008 2007{
local quota = cond(`ay'==2008,1,0)*1950 /// H20
            + cond(`ay'==2007,1,0)*650 
replace tr20220000 = 34.6 if ayear == `ay' & cnames == "Chile" & Chile_q <= `quota'
replace tr20230010 = 34.6 if ayear == `ay' & cnames == "Chile" & Chile_q <= `quota'
replace tr20230020 = 34.6 if ayear == `ay' & cnames == "Chile" & Chile_q <= `quota'
replace tr20230030 = 34.6 if ayear == `ay' & cnames == "Chile" & Chile_q <= `quota'
replace tr20230090 = 34.6 if ayear == `ay' & cnames == "Chile" & Chile_q <= `quota'
}

*** Drop others
foreach s of local others{
drop kgrm`s'
*drop kjpy`s'
}

* Tariff Duty Revenue (each meat)
foreach s of local beef{
gen tdrev`s' = kjpy`s'*(tr`s'/100) // tariff duty in kjpy
}
* Tariff Duty revenue (total meat)
egen tdrev_all = rowtotal(tdrev*) // 
	bysort cnum year (month) : gen ytdrev_all = sum(tdrev_all) // cumulate 12 months
* All meat weight imported
egen kgrm_all = rowtotal(kgrm*) // OK because we dorpped kgrm for others
	bysort cnum year (month) : gen ykgrm_all = sum(kgrm_all) // cumulate 12 months
* All meat value imported
egen kjpy_all = rowtotal(kjpy*) // OK because we dorpped kgrm for others
	bysort cnum year (month) : gen ykjpy_all = sum(kjpy_all) // cumulate 12 months
drop kgrm2* kjpy2*
drop tr2* tdrev2*

* Unit value
gen cif = kjpy_all/kgrm_all*1000 // JPY/kg
	gen ycif = ykjpy_all/ykgrm_all*1000 // JPY/kg
* Tariff duty
gen td = tdrev_all/kgrm_all*1000 // JPY/kg
	gen ytd = ytdrev_all/ykgrm_all*1000 // JPY/kg
* Price
gen pr = cif + td
	gen ypr = ycif + ytd
* Tariff rates
gen tr = td/cif
	gen ytr = ytd/ycif
* Sare
gen kjpy_dem = pr*kgrm_all/1000
egen csum = sum(kjpy_dem), by(time) // country-sum
*	egen ycsum = sum(ykjpy_all), by(time) // country-sum
gen lnsh = ln(kjpy_dem/csum)
*	gen lnysh = ln(ykjpy_dem/ycsum)
gen lnpr = ln(pr)
*	gen lnypr = ln(ypr)

	
* REGRESSION
tabulate time, generate(t)
tabulate cnum, generate(c)
xtset cnum time
gen lnjpylcc = ln(jpylcc)
bysort cnum year (month): gen yjpylcc = sum(jpylcc)
gen lnyjpylcc = ln(yjpylcc)



* Count # of time observatios for each panel
forvalues i = 1/86 {
*di "c",`i'
count if lnsh !=. & c`i' ==1
}

********************************************************************************
* Alternative test for endogeneity
gen lnce = ln(cif) - lnjpylcc
xtset cnum time
xtreg lnce lnjpylcc if ( ///
c79==1|c81==1|c53==1|c52==1|c83==1|c54==1|c31==1|c59==1|c67==1| ///
c29==1|c57==1|c39==1|c28==1|c69==1|c37==1|c58==1|c41==1|c1==1 | ///
c27==1|c42==1|c26==1|c63==1 ///
), fe // NOT relevant => No feedback
xtreg lnce lnjpylcc if ( ///
c79==1|c81==1|c53==1|c52==1|c83==1|c54==1|c31==1|c59==1|c67==1| ///
c29==1|c57==1|c39==1|c28==1|c69==1|c37==1|c58==1|c41==1|c1==1 | ///
c27==1|c42==1|c26==1|c63==1 ///
), fe vce(robust) // NOT relevant => No feedback
* Result: some feedback EXISTS
********************************************************************************

* MAIN REGRESSION * (and put the output into a .log file)
* FE(LS)
log using beef_sigma_LS.log
xtreg lnsh t1-t300 lnpr if ( ///
c79==1|c81==1|c53==1|c52==1|c83==1|c54==1|c31==1|c59==1|c67==1| ///
c29==1|c57==1|c39==1|c28==1|c69==1|c37==1|c58==1|c41==1|c1==1 | ///
c27==1|c42==1|c26==1|c63==1 ///
), fe vce(robust)
log close 
* FE(IV)
log using beef_sigma_IV.log
xtivreg2 lnsh t1-t300 (lnpr = lnjpylcc lnyjpylcc) if ( ///
c79==1|c81==1|c53==1|c52==1|c83==1|c54==1|c31==1|c59==1|c67==1| ///
c29==1|c57==1|c39==1|c28==1|c69==1|c37==1|c58==1|c41==1|c1==1 | ///
c27==1|c42==1|c26==1|c63==1 ///
), fe robust bw(5) endog(lnpr)
log close 
* DELTA METHOD
log using beef_index.log
nlcom (sigma: 1-_b[lnpr])
forvalues i = 1/300{
nlcom (pindx: exp(-((0)+_b[t`i'])/_b[lnpr]))
}
log close


****************************************************
* MAC UNIX
* grep "pindx" beef.log | grep "|" > beef.txt
****************************************************

